﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestorDeFlotas.Core;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmCliente
{
    public partial class AbmCliente : FormABMBase
    {
        public AbmCliente(ModoAbm modoAbm)
            : base(modoAbm)
        {
            InitializeComponent();
        }

        public Cliente MyCliente { get; set; }

        private void AbmCliente_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'gD1C2012DataSet.TL_Clientes' table. You can move, or remove it, as needed.
            this.tL_ClientesTableAdapter.Fill(this.gD1C2012DataSet.TL_Clientes);
            this.HabilitarControles();
        }

        private void HabilitarControles()
        {
            this.btnSeleccionar.Visible = this.modoAbm == ModoAbm.Consulta;
            this.boton_agregar.Visible = this.modoAbm != ModoAbm.Consulta;
            this.dataGridView1.Columns["seleccionar_cliente"].Visible = this.modoAbm != ModoAbm.Consulta;
            this.dataGridView1.Columns["eliminar_cliente"].Visible = this.modoAbm != ModoAbm.Consulta;
        }

        private void RefrescarDataGrid()
        {
            // Codigo para refrescar el DataGridView
            this.gD1C2012DataSet.GetChanges();
            this.tL_ClientesTableAdapter.Fill(this.gD1C2012DataSet.TL_Clientes);

        }
        private void boton_limpiar_Click(object sender, EventArgs e)
        {
            textBox_apellido.Clear();
            textBox_DNI.Clear();
            textBox_nombre.Clear();
            this.RefrescarDataGrid();

        }
        private void boton_buscar_Click(object sender, EventArgs e)
        {   
            int DNI;
            if (this.textBox_DNI.Text == "")
            {
                this.tL_ClientesTableAdapter.FillBy_DatosIngresados_SinDNI(this.gD1C2012DataSet.TL_Clientes, string.Format("%{0}%", this.textBox_nombre.Text), string.Format("%{0}%", this.textBox_apellido.Text));
            }
            else
            {
                DNI = Convert.ToInt32(this.textBox_DNI.Text);
                this.tL_ClientesTableAdapter.FillBy_DatosIngresados_ConDNI(this.gD1C2012DataSet.TL_Clientes, string.Format("%{0}%", this.textBox_nombre.Text), string.Format("%{0}%", this.textBox_apellido.Text), DNI);
            }
        }
        
        private void boton_agregar_Click(object sender, EventArgs e)
        {
            DatosCliente datosCliente = new DatosCliente(ModoAbm.Alta);
            if (datosCliente.ShowDialog() == DialogResult.OK)
            {
                RefrescarDataGrid();
            }
        }

        private void boton_cerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || (e.ColumnIndex !=
                   dataGridView1.Columns["seleccionar_cliente"].Index && e.ColumnIndex != dataGridView1.Columns["eliminar_cliente"].Index)) return;

            // Retrieve the client ID.
            Int32 cli_ID = (Int32)dataGridView1["cli_codigo", e.RowIndex].Value;

            if (e.ColumnIndex == dataGridView1.Columns["seleccionar_cliente"].Index)
            {
                DatosCliente datosCliente = new DatosCliente(ModoAbm.Modificacion, new Cliente(cli_ID).Cargar());
                if (datosCliente.ShowDialog() == DialogResult.OK)
                {
                    RefrescarDataGrid();
                }
            }
            else if (e.ColumnIndex == dataGridView1.Columns["eliminar_cliente"].Index)
            {
                if ((Boolean)dataGridView1["cli_activo", e.RowIndex].Value)
                {
                    new Cliente(cli_ID).SetInactivo();
                    RefrescarDataGrid();
                }
            }
        }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 0) MyCliente = null;

            this.MyCliente = new Cliente((int)dataGridView1["cli_codigo", dataGridView1.SelectedRows[0].Index].Value);
            this.DialogResult = DialogResult.OK;
        }
    }
}
